 class ListNode {
    int val;
   ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
  }
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
    public ListNode ReverseList (ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode tail=null;
        ListNode pre=head;
        ListNode cur=head.next;
        while(pre.next!=null){
            pre.next=tail;
            tail=pre;
            pre=cur;
            cur=pre.next;
        }
        pre.next=tail;
        return pre;
    }
    public ListNode reverseBetween (ListNode head, int m, int n) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        for (int i = 0; i < m - 1; i++) {
            pre = pre.next;
        }
        ListNode rigthNode = pre;
        for (int i = 0; i < n - m + 1; i++) {
            rigthNode = rigthNode.next;
        }
        ListNode leftNode = pre.next;
        ListNode cur = rigthNode.next;
        pre.next = null;
        rigthNode.next = null;
        reverseLinkedList(leftNode);
        pre.next = rigthNode;
        leftNode.next = cur;
        return dummyNode.next;
    }
    private void reverseLinkedList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode Cur_next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = Cur_next ;
        }
    }
}